package com.jf.identity.service.impl;

import com.jf.identity.dao.visexamid.ExamineeDao;
import com.jf.identity.entity.ExamBaseParam;
import com.jf.identity.entity.ExamBaseParamValidator;
import com.jf.identity.entity.Examinee;
import com.jf.identity.holder.ExamBaseParamHolder;
import com.jf.identity.service.ExamineeService;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.nio.charset.Charset;

/**
 * Created by x on 2018/4/12.
 */
@Service
public class ExamineeServiceImpl implements ExamineeService {
    @Resource
    private ExamineeDao examineeDao;
    @Resource
    private ExamBaseParamHolder examBaseParamHolder;
    @Resource
    private ExamBaseParamValidator examBaseParamValidator;
    @Override
    public boolean insertExamineeSignInfo(Examinee examinee) {
        ExamBaseParam examBaseParam = examBaseParamHolder.getExamBaseParam();
        ExamBaseParam.isBlank(examBaseParam);
        examBaseParamValidator.validate(examBaseParam,ExamBaseParam.EXAM_ID);
        //加入uid
        String uid = getExamineeSignUid(examinee, examBaseParam);
        examinee.setUid(uid);
        //加入数据库
        int n = examineeDao.insertExamineeSignInfo(examBaseParam, examinee);
        return n != 0;
    }

    /**
     * v_rel_examinee_sign uid = MD5(examId+idCardNum)
     *
     * @param examinee      examId
     * @param examBaseParam idCardNum
     * @return uid
     */
    private String getExamineeSignUid(Examinee examinee, ExamBaseParam examBaseParam) {
        String md5 = examBaseParam.getExamId() + examinee.getIdCardNum();
        return DigestUtils.md5DigestAsHex(md5.getBytes(Charset.forName("UTF-8")));
    }

}
